package com.yang.demo.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Handler;
import android.view.View;

import java.util.Random;

import static android.os.Looper.getMainLooper;

/**
 * Created by yangwenwen on 2018/3/16.
 */
public class MyView extends View {
    private Handler mHandler;

    private int mColor;

    private float cx;

    private float cy;

    private float radius;

    public MyView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
        mHandler = new Handler(getMainLooper());

        setBackgroundColor(Color.BLACK);

        Thread mThread = new Thread(new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                MyView.this.invalidate();
                mHandler.postDelayed(this, 100);
            }
        });
        mThread.start();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        // TODO Auto-generated method stub
        super.onDraw(canvas);
        update();
        Paint p = new Paint();
        p.setColor(mColor);
        canvas.drawCircle(cx, cy, radius, p);
    }

    private void update() {
        Random random = new Random();
        cx = (float) (random.nextInt(200));        //  随机生成圆心横坐标（0至200）
        cy = (float) (random.nextInt(400));        //  随机生成圆心纵坐标（0至400）
        radius = (float) (10 + random.nextInt(90));      //  随机生成圆的半径（10至100）

        int r = random.nextInt(256);
        int g = random.nextInt(256);
        int b = random.nextInt(256);
        mColor = Color.rgb(r, g, b);                    // 随机生成颜色
    }
}